package com.gui.juc.base.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class ThreadUnsafeDemo1 {

    static int var = 0;

    private static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch = new CountDownLatch(8);

        for (int i = 0; i < 8; i++) {
            new Thread(() -> {
               synchronized (lock) {
                   for (int j = 0; j < 2000; j++) {
                       var++;
                   }
               }
                latch.countDown();
            }).start();
        }

        latch.await();
        log.debug("var 的值为：{}", var);
    }
}
